package cn.service.productService.ProductServiceImpl;

import cn.service.productService.CategoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import entity.productEntity.Category;
import entity.Result;
import mapper.productMapper.CategoryMapper;
import mapper.productMapper.PmsProductMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 朱颢阳
 * @description
 * @time 2024/8/21 20:15
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    CategoryMapper mapper;
    @Resource
    PmsProductMapper pmsProductMapper;
    @Override
    public Result selsectAll(Integer parent_id,Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Category> page= (Page<Category>) mapper.selectAll(Long.valueOf(parent_id));
        System.out.println(page.getTotal());
        return Result.success((int)page.getTotal(),page.getResult());
    }
    @Override
    public Result insertCategory(Category category) {
        if(mapper.insert(category)>0){
            return Result.success("添加成功");
        }else{
            return Result.failed(500,"添加失败！");
        }
    }
    @Override
    public Result updataCategory(Category category) {
        if(mapper.updateByPrimaryKeySelective(category)>0){
            return Result.success("修改成功");
        }else{
            return Result.failed(500,"修改失败！");
        }
    }
    @Override
    public Result deleteCategory(Integer id) {
        if (pmsProductMapper.selectCateProduct(id)>0){
            return Result.failed(500,"该字段还有商品存在！删除失败！");
        }else {
            if (mapper.deleteByPrimaryKey(Long.valueOf(id))>0){
                return Result.success("删除成功");
            }else{
                return Result.failed(500,"删除失败！");
            }
        }
    }

    @Override
    public Result selectparents() {
        return Result.success(200,mapper.selectparents(0));
    }

    @Override
    public Result getoptions() {
        List<Category> parents = mapper.selectparents(0);
        for (Category parent : parents) {
            parent.setChildren(mapper.selectparents(Math.toIntExact(parent.getId())));
        }
        System.out.println(parents);
        return Result.success(200,parents);
    }

}
